package wjb.test.mawen.mawen.provider;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import wjb.test.mawen.mawen.dto.AccessUser;
import wjb.test.mawen.mawen.dto.TokenInfo;

import java.io.IOException;

@Component
public class GiteeProvide {
    @Value("${gitee.authorizeUrl}")
    private  String authorizeUrl;
    @Value("${gitee.client_id}")
    private  String client_id;
    @Value("${gitee.redirect_uri}")
    private  String redirect_uri;
    @Value("${gitee.client_secret}")
    private  String client_secret;
    @Value("${gitee.tokenUrl}")
    private  String tokenUrl;
    @Value("${gitee.getUserUrl}")
    private  String getUserUrl;

    public static final MediaType MEDIA_TYPE = MediaType.get("application/json;charset=utf-8");
    OkHttpClient client = new OkHttpClient();

    public TokenInfo getAccessToken(String code) {
        TokenInfo tokenInfo=null;
        StringBuffer url=new StringBuffer();
        url.append(tokenUrl);
        url.append("?grant_type=authorization_code");
        url.append("&code="+code);
        url.append("&client_id="+client_id);
        url.append("&redirect_uri="+redirect_uri);
        url.append("&client_secret="+client_secret);
        System.out.println("url_thonk:"+url.toString());
        RequestBody requestBody=RequestBody.create(MEDIA_TYPE, "{}");
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url.toString())
                .build();
        try(Response response = client.newCall(request).execute()) {
            String string=((Response) response).body().string();
            tokenInfo=JSON.parseObject(string, TokenInfo.class);
            return tokenInfo;
        }catch (IOException e){
            e.printStackTrace();
        }
        return  tokenInfo;
    }

    //刷新登录许可信息
    public TokenInfo refresh_token(String refresh_token){
        TokenInfo tokenInfo=null;
        String url=tokenUrl+"?grant_type=refresh_token&refresh_token="+refresh_token;
        RequestBody requestBody=RequestBody.create(MEDIA_TYPE, "{}");
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();
        try(Response response = client.newCall(request).execute()) {
            String string=((Response) response).body().string();
            tokenInfo=JSON.parseObject(string, TokenInfo.class);
            return tokenInfo;
        }catch (IOException e){
            e.printStackTrace();
        }
        return tokenInfo;
    }

    public AccessUser getUserInfo(String access_token){
        AccessUser accessUser=null;
        Request request=new Request.Builder()
                .url(getUserUrl+"?access_token="+access_token)
                .build();
        try {
            Response response=client.newCall(request).execute();
             String string=response.body().string();
             accessUser= JSON.parseObject(string,AccessUser.class);
             return accessUser;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accessUser;
    }


}

